<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>

<body>
    <div id="app">
        <h1 v-red>我是标题</h1>
        <h1 v-color="'green'">我是标题2动态颜色</h1>
        <input v-mobile type="text" v-model ='phone'>
    </div>


    <script>
        // 自定义指令 可以极大拓宽vue的 自定义复用 指令
        // Vue.directive('red', {
        //     // 绑定元素的时候自动调用
        //     inserted: function (el) {
        //         console.log('el', el)
        //         el.style.color = 'red'
        //     }
        // })

        Vue.directive('color', {
            // 绑定元素的时候自动调用
            inserted: function (el, binding) {
                console.log('el', el)
                console.log('binding.value', binding.value)
                el.style.color = binding.value
            }
        })

        var app = new Vue({
            el: '#app',
            data: {
                message: 'Hello Vue22222!',
                phone:''
            },
            directives: {
                red: {
                    inserted: function (el) {
                        console.log('el', el)
                        el.style.color = 'orange'
                    }
                },
                mobile:{
                    // 在指令所在节点数据更新的时候调用
                    update:function(el) {
                        console.log('mobile',el)
                        // 
                        if((/^1[3-9]\d{9}$/).test(el.value)) {
                            el.style.color='black'
                        }else {
                            el.style.color='red'
                        }
                    }
                }
            }
        })

        console.log('app', app)

    </script>
</body>

</html>